home *** CD-ROM | disk | FTP | other *** search
- Path: druid.borland.com!usenet
- From: "James J. Hartley" <jamesh@borland.com>
- Newsgroups: comp.lang.c++
- Subject: Re: HELP Borland C++ 5.0 pointer problem
- Date: Fri, 12 Apr 1996 12:47:20 -0700
- Organization: Borland International
- Message-ID: <316EB348.3220@borland.com>
- References: <4klke8$7mb@dub-news-svc-2.compuserve.com>
- NNTP-Posting-Host: yeti.borland.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.01 (Win95; I)
-
- Andy Graham wrote:
- >
- > Could you please tell me why the function below compiles and operates
- > correctly under win16 but not win32 ?
- >
- > char* test_function(char *in_data) {
- >
- > char buffer[20];
- > strcpy(buffer, in_data); // copy in_data to buffer
- > in_data = buffer; // set pointer to buffer
- >
- > return(in_data);
- >
- > } // end test_function function
-
- This is dangerous code under any platform. The stack memory associated with
- buffer will be available to any other function once the scope of test_function()
- is exited. I don't know the specifics of your application, but there are
- situations where bad coding is not brutally dealt with by the underlying OS;
- consider yourself lucky that this code worked under Win16 in the first place.
- One simple fix which may prove useful is to move buffer from the stack to the
- data segment(s) by declaring it static.
-
- Jim
-